
let win;

const getBounds = (x, y) => {
  let displayMatching = utools.getDisplayNearestPoint({x,y});
  let display = utools.getPrimaryDisplay();
  let displayMatchingScaleFactor =  displayMatching.scaleFactor;
  let displayScaleFactor =  display.scaleFactor;

  let difference = displayScaleFactor - displayMatchingScaleFactor;
  return function (width, height){
    if(difference > 0){
      width = parseInt(width * (1 + difference));
      height = parseInt(height * (1 + difference));
      return {
        x, y, width, height
      }
    }else if(difference < 0){
      width = parseInt(width / (1 - difference));
      height = parseInt(height / (1 - difference));
      return {
        x, y, width, height
      }
    }else{
      return {
        x,
        y,
        width,
        height
      }
    }
  }
}
const calculatePosition = (info) => {
  let { x, y, width, height} = info;
  let {width: childWidth, height: childHeight} = window.conterSetting || {
    width: 250,
    height: 400
  }
  const display = utools.getDisplayNearestPoint({x,y});
  const boundsWidth = display.bounds.width;
  const boundsHeight = display.bounds.height;

  if(window.conterSetting && window.conterSetting.conter){
    x = boundsWidth / 2 - childWidth / 2;
    y = boundsHeight / 2 - childHeight / 2;
    return { x,y, width: childWidth, height: childHeight }
  }

  if(x + width + childWidth + 5 > boundsWidth){
    x = x - childWidth - 5
  }else{
    x = x + width + 5
  }

  let obj = getBounds(x,y)(childWidth, childHeight)
  return { x, y, width: childWidth, height: childHeight, ...obj }
}

window.openSettingWin = (position, url) => {
  let { x, y, width, height } = calculatePosition(position);
  const ubWindow = utools.createBrowserWindow(
    url + "setting.html",
    {
      title: "编辑",
      skipTaskbar: true,
      frame: false,
      backgroundColor: "rgba(255,255,255,0)",
      transparent: true,
      titleBarStyle: "hidden",
      // resizable: false,
      width: width,
      height: height || 365,
      x,
      y,
      webPreferences: {
        preload: url + "preload.js",
        spellcheck: false,
      },
    },
    () => {
      ubWindow.show()
      ubWindow.setBackgroundColor("rgba(255,255,255,0)");
      // ubWindow.webContents.openDevTools();
      ipcRenderer.sendTo(ubWindow.webContents.id, "senderId")
      let data = window.getData()
      ipcRenderer.sendTo(ubWindow.webContents.id, "init", data)
    }
  );


  win = ubWindow;
};

ipcRenderer.on("win", (event, type) => {
  if(!win)return;
  switch (type) {
    case "close":
      win.destroy();
      win = null;
      break;
    // case "setEmojin":
      
    default:
      break;
  }
});



ipcRenderer.on("position", (event, info) => {
  if(win)return
  let {data, url} = info
  window.openSettingWin(data,url)
})


ipcRenderer.on("setSize", (event, data) => {
  ipcRenderer.sendTo(window.senderId, 'setSize', data);
})

window.closeSettingWin = () => {
  if(win){
    win.destroy();
    win = null;
  }
}
